# -*- coding: utf-8 -*
# pylint: disable=unsubscriptable-object
"""

"""
import pickle
import matplotlib.pyplot as plt
from pathlib import Path
from scipy.sparse import csr_matrix
import numpy as np

from PIL import Image


def getAllImages(sub_dir):
    images = []
    for fp in sub_dir.iterdir():
        image = Image.open(fp)
        image = np.array(image)
        images.append(image)
    return images


def showImage(img: Image):
    # img = np.array(img)
    plt.imshow(img)
    plt.show()


def sparse(images: list):
    sparse_images = []
    for img in images:
        new_img = csr_matrix(np.array(img))
        sparse_images.append(new_img)
    return sparse_images


def main():
    base_dir = r'./dataset/Origin/题目一'
    data = {0: None, 1: None, 2: None}
    base_dir = Path(base_dir)
    for d in ['1']:
        sub_dir = base_dir / d
        images = getAllImages(sub_dir)
        sparse_images = sparse(images)
        target = int(d) - 1
        data[target] = sparse_images[0:1]
    print('Dumping...')
    with open(r'./dataset/data.pkl', 'wb') as f:
        pickle.dump(data, f)


if __name__ == '__main__':
    main()
